package com.gostudy.gulimall.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.gostudy.common.valid.AddGroup;
import com.gostudy.common.valid.UpdateGroup;
import com.gostudy.common.valid.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gostudy.gulimall.product.entity.BrandEntity;
import com.gostudy.gulimall.product.service.BrandService;
import com.gostudy.common.utils.PageUtils;
import com.gostudy.common.utils.R;

import javax.validation.Valid;

/**
 * 品牌
 *
 * @author 53Hertz
 * @email lizhenwei188@foxmail.com
 * @date 2021-04-03 14:46:40
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
//    @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:brand:save") @Valid 开启验证功能
//    BindingResult result 紧跟要校验的对象，返回校验的结果
//    @Validated(AddGroup.class) 只校验添加组里的规则
    public R save(@Validated(AddGroup.class) @RequestBody BrandEntity brand /*,BindingResult result*/){
        //如果得到错误
//        if (result.hasErrors()) {
//            Map<Object, Object> map = new HashMap<>();
//            //遍历错误信息
//            result.getFieldErrors().forEach((item) -> {
//                //获取到的错误提示消息
//                String message = item.getDefaultMessage();
//                // 获取到是那个字段有错误消息
//                String field = item.getField();
//                map.put(field,message);
//            });
//            return R.error(400,"提交的数据不合法").put("data", map);
//        } else {
//
//        }
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("product:brand:update")
    public R update(@Validated(UpdateGroup.class)@RequestBody BrandEntity brand){
		brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 修改状态
     */
    @RequestMapping("/update/status")
//    @RequiresPermissions("product:brand:update")
    public R updateStatus(@Validated(UpdateStatusGroup.class)@RequestBody BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }
//    用户登录名称 gulimall@1325815525111500.onaliyun.com
//    AccessKey ID LTAI5t76UprkMWsBGAi2bKox
//    AccessKey Secret OGmnqIlM4a2mQI7v4AcpBcaKJj1fqI
}
